package shuai.test;

import java.text.Normalizer.Form;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import shuai.model.Emp;

public class Demo {
	Session session=null;
	@Before
	public void initSession(){
		Configuration configuration=new Configuration().configure();
		SessionFactory factory=configuration.buildSessionFactory();
		session=factory.openSession();
	}
	@Test
	public void queryAll(){
		String hql="from Emp";
		Query query=session.createQuery(hql);
		List<Emp> emps=query.list();
		for(Emp e:emps){
			System.out.println(e.getEname());
		}
	}
	@Test
	public void queryAll1(){
		String hql="from Emp e where e.ename like '%S%' and e.hiredate>'1-10月-1985'";
		Query query =session.createQuery(hql);
		List<Emp> emps=query.list();
		for(Emp e:emps){
			System.out.println(e.getEname());
		}
	}
	
	@Test
	public void queryMany(){
		String hql="select e.ename,e.sal from Emp e ";
		Query query =session.createQuery(hql);
		List<Object[]> objects=query.list();
		for(Object[] o:objects){
			System.out.println("姓名:"+o[0]+",工资:"+o[1]);
		}
	}
	
	@Test
	public void queryCus(){
		String hql="select new Emp(e.ename,e.job,e.sal) from Emp e";
		Query query=session.createQuery(hql);
		List<Emp> emps=query.list();
		for(Emp e:emps){
			System.out.println(e.getEname()+e.getJob()+e.getSal());
		}
	}
	@Test
	public void queryPos(){
		String hql="select e from Emp e where ename like ? and sal>?";
		Query query =session.createQuery(hql);
		query.setString(0, "%S%");
		query.setDouble(1, 1000);
		List<Emp> emps=query.list();
		for(Emp e:emps){
			System.out.println(e.getEname()+e.getJob()+e.getSal());
		}
	}
	
	@After
	public void destorySession(){
		session.close();
	}
}
